home *** CD-ROM | disk | FTP | other *** search
- Path: news.nask.org.pl!usenet
- From: piotrpar@blue.maloka.waw.pl (Piotr Parlewicz)
- Newsgroups: comp.lang.c++
- Subject: Re: \\ question fo iostream people //
- Date: Sun, 24 Mar 1996 00:06:26 GMT
- Organization: Research and Academic Computer Network
- Message-ID: <4j23ju$77v@bilbo.nask.org.pl>
- References: <4istul$jon@thales.nmia.com>
- NNTP-Posting-Host: s114.maloka.waw.pl
- X-Newsreader: Forte Free Agent 1.0.82
-
- ghealton@nmia.com (Gilbert Healton) wrote:
-
- >How many good ways are there to solve this problem (I am looking for one):
-
- > Set up some type of I/O stream (or stream reference and stream) that
- > allows a program to write to either standard out or a selected file.
- > The ability to switch at will at different points of the program is
- > vital. Code that works on different compilers is preferred.
-
- >In C the code is simple and portable:
-
- > FILE *Stream = (FILE *)NULL;
- > char *StreamPath;
- > . . .
- > if ( SomeCondition )
- > { // time to open a file
- > if ( Stream ) //but, first, if already open
- > fclose( Stream ); ////close the puppy
- > if ( *StreamPath )
- > Stream = fopen( StreamPath, "w" ); // disk file
- > else
- > Stream = stdout; // stdout
- > }
-
- >If I get some good answers, I'll put them in FAQ format and forward them
- >to the C++ FAQ.
-
- >--
- >----- Computer Consulting / Web Pages ----- http://www.nmia.com/~ghealton/
- >These opinions are my own. Life is learning and I may retract, modify,
- >even attack, my previous ideas at any time without notice.
-
-
- /*
- Here is a simple solution that parallels the behaviour of your C code,
- minus the fclose of stdout.
- In Visual C++ 2.0, cout is an object of the class
- 'ostream_withassign',
- which allows the cout object to have its output reassigned to another
- stream using the overloaded '=' operator.
- But I dont know if that would be portable, so this solution, though
- less
- elegant is probably portable.
-
- Piotr Parlewicz piotrpar@blue.maloka.waw.pl */
-
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <fstream.h>
-
-
- // without arguments, prints to screen
- // with argument , prints to file named like the passed argument
- int main( int argc, char **argv ) {
-
- char *strm_path=NULL;
- int SomeCondition=0 ;
- ostream *ofs ; // common base class for cout and ofstream
- int ofs_file=0; // the primitive solution to knowing if delete is
- //needed
-
- // for demo only
- if( argc == 2 )
- strm_path = argv[1] ;
-
- if( argc <= 2 )
- SomeCondition = 1 ;
-
- // ...
- if( SomeCondition ) {
-
- if( ofs_file )
- delete ofs ; // this was an ofstream, BTW the C solution tried to
-
- // fclose stdout
-
- // this way, the file is rewritten every time, just like in the
- // C example, if you want appending, use ios::app instead
- // of ios::trunc
-
- ofs = strm_path ?
- (ofs_file=1,(ostream *) new ofstream( strm_path, ios::trunc ) ) :
- (ofs_file=0,(ostream *) &cout ); // cout exists globaly
-
- // do your output to whatever was set up eariler
- *ofs << "This is a message that will go to file or console\n" ;
- }
-
-
- // upon program exit insure the file output is flushed
- if( ofs_file )
- delete ofs ;
- return(0);
- }
-
-
-